﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace Zero.UControl.Component
{
    /// <summary>
    /// RateControl.xaml 的交互逻辑
    /// </summary>
    public partial class RateControl : UserControl
    {
        public RateControl()
        {
            InitializeComponent();
            UpdateStars();
        }


        #region 普通属性
        public ObservableCollection<StarViewModel> Stars { get; set; } = new ObservableCollection<StarViewModel>();

        #endregion

        #region 依赖属性
        public double Score
        {
            get => (double)GetValue(ScoreProperty);
            set => SetValue(ScoreProperty, value);
        }

        public static readonly DependencyProperty ScoreProperty =
          DependencyProperty.Register(nameof(Score), typeof(double), typeof(RateControl),
              new PropertyMetadata(0.0, OnScoreChanged));


        public Geometry ShapeGeometry
        {
            get { return (Geometry)GetValue(ShapeGeometryProperty); }
            set { SetValue(ShapeGeometryProperty, value); }
        }

        public static readonly DependencyProperty ShapeGeometryProperty =
            DependencyProperty.Register("ShapeGeometry", typeof(Geometry), typeof(RateControl),
                new PropertyMetadata(Geometry.Parse("M10,1 L12,7 H18 L13,11 L15,17 L10,13 L5,17 L7,11 L2,7 H8 Z")));


        public Brush LabelColor
        {
            get { return (Brush)GetValue(LabelColorProperty); }
            set { SetValue(LabelColorProperty, value); }
        }

        public static readonly DependencyProperty LabelColorProperty =
            DependencyProperty.Register("LabelColor", typeof(Brush), typeof(RateControl),
                new PropertyMetadata(new SolidColorBrush(Colors.Orange)));


        public Brush FillColor
        {
            get { return (Brush)GetValue(FillColorProperty); }
            set { SetValue(FillColorProperty, value); }
        }

        public static readonly DependencyProperty FillColorProperty =
            DependencyProperty.Register("FillColor", typeof(Brush), typeof(RateControl), 
                new PropertyMetadata(new SolidColorBrush(Colors.Orange)));


        public Brush BackFillColor
        {
            get { return (Brush)GetValue(BackFillColorProperty); }
            set { SetValue(BackFillColorProperty, value); }
        }

        public static readonly DependencyProperty BackFillColorProperty =
            DependencyProperty.Register("BackFillColor", typeof(Brush), typeof(RateControl),
                new PropertyMetadata(new SolidColorBrush((Color)ColorConverter.ConvertFromString("#ddd"))));


        /// <summary>
        /// 小数点位数
        /// </summary>
        public int DigitalCount
        {
            get { return (int)GetValue(DigitalCountProperty); }
            set { SetValue(DigitalCountProperty, value); }
        }

        public static readonly DependencyProperty DigitalCountProperty =
            DependencyProperty.Register("DigitalCount", typeof(int), typeof(RateControl), new PropertyMetadata(1));


        #endregion

        #region 事件
        /*
            处理思想：
                鼠标进入移动时，记录鼠标位置，然后得到当前评分，动态更新UI
                鼠标点击后，记录当前位置，计算当前评分
                鼠标移出后，根据当前评分，动态更新UI
         */
        private bool _isMouseOver;

        private void Root_MouseMove(object sender, MouseEventArgs e)
        {
            if (!_isMouseOver)
            {
                _isMouseOver = true;
            }

            Point pos = e.GetPosition(InteractiveArea);
            double relativeX = pos.X;
            double newScore = Math.Max(0, Math.Min(5, relativeX / 24.0));
            UpdateStars(Math.Round(newScore,DigitalCount));
        }

        private void Root_MouseLeave(object sender, MouseEventArgs e)
        {
            _isMouseOver = false;
            UpdateStars(Score); // 恢复实际评分
        }

        private void Root_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            Point pos = e.GetPosition(InteractiveArea);
            double relativeX = pos.X;
            double newScore = Math.Max(0, Math.Min(5, relativeX / 24.0));
            this.SetCurrentValue(ScoreProperty, Math.Round(newScore, DigitalCount));
        }

        #endregion

        #region 本地方法

        private static void OnScoreChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            if (d is RateControl control)
            {
                control.UpdateStars();
            }
        }

        private void UpdateStars(double? previewScore = null)
        {
            double scoreToUse = previewScore ?? Score;

            Stars.Clear();
            for (int i = 0; i < 5; i++)
            {
                double starFill = Math.Max(0, Math.Min(1, scoreToUse - i));
                Stars.Add(new StarViewModel
                {
                    FillRect = new Rect(0, 0, 20 * starFill, 20)
                });
            }
        }
        #endregion
    }

    public class StarViewModel
    {
        public Rect FillRect { get; set; }
    }
}
